package controller

import (
	"api_interface/entity"
	"api_interface/service"
	"fmt"
	"net/http"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

// 发起提问

func CreateQues() gin.HandlerFunc {
	return func(c *gin.Context) {
		var ques entity.QuestionInfo              //用于传给service层的结构体
		if err := c.BindJSON(&ques); err != nil { //将body里的JSON格式数据解构到ques里
			c.JSON(http.StatusOK, gin.H{
				"code": 1,
				"msg":  "error to get json body",
				"data": nil,
			})
			return
		}
		currentTime := time.Now()
		timeString := currentTime.Format("2006-01-02 15:04:05") //定义时间存储样式
		ques.Tim = timeString                                   //将时间赋值给ques.tim
		err := service.CreateQues(&ques)                        //调用service层函数，将ques传给函数用于创建question
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 1,
				"msg":  "error to create question",
				"data": nil,
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{ //无error，返回success
			"code": 0,
			"msg":  "success",
			"data": nil,
		})
	}
}

// 查看提问箱中的所有提问

func GetQuesFromBox() gin.HandlerFunc {
	return func(c *gin.Context) {
		bid1 := c.DefaultQuery("bid", "")            //接收bid
		page_num1 := c.DefaultQuery("pageNum", "")   //接收pageNum
		page_size1 := c.DefaultQuery("pageSize", "") //接收pageSize
		aid1 := c.DefaultQuery("aid", "")            //接收aid
		bid, _ := strconv.Atoi(bid1)                 //将string转为int
		aid, _ := strconv.Atoi(aid1)
		page_num, _ := strconv.Atoi(page_num1)
		page_size, _ := strconv.Atoi(page_size1)
		fmt.Println(111111)
		questionwithliked, err := service.GetQuesFromBox(aid, bid) //调用service层查看提问箱中的所有提问
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 1,
				"msg":  "fail to get the questions from the box",
				"data": nil,
			})
			return
		}
		// length := len(*boxes)
		// var right int
		// right = (page + 1) * (page_size)
		// if len(*boxes) < (page+1)*(page_size) { //最后一页没有一页的量
		// 	right = len(*boxes)
		// }
		// box := (*boxes)[page*page_size : right]

		length := len(*questionwithliked)
		var cutquestionwithliked []entity.QuestionInfoWithLiked
		if (page_num+1)*page_size > length {
			cutquestionwithliked = (*questionwithliked)[page_num*page_size : length]
		} else {
			cutquestionwithliked = (*questionwithliked)[page_num*page_size : (page_num+1)*page_size]
		}
		type QuesData struct {
			Total     int                            `json:"total"`
			Questions []entity.QuestionInfoWithLiked `json:"questions"`
		}
		data := QuesData{
			Total:     length,
			Questions: cutquestionwithliked,
		}
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "success",
			"data": data,
		})
	}
}

// 查看提问详细内容
type IdGet struct {
	Id int `json:"id"`
}

func GetQuesById() gin.HandlerFunc {
	return func(c *gin.Context) {
		iid := c.DefaultQuery("id", "") //接收id
		id, _ := strconv.Atoi(iid)
		question, err1, err2 := service.GetQuesById(id) //调用service层函数查看提问详细内容并传给question
		if err1 != nil {                                //接收失败
			c.JSON(http.StatusOK, gin.H{
				"code": 1,
				"msg":  "fail to get the question",
				"data": nil,
			})
			return
		}
		if err2 != nil { //增加views失败
			c.JSON(http.StatusOK, gin.H{
				"code": 1,
				"msg":  "fail to add views",
				"data": nil,
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{ //success
			"code": 0,
			"msg":  "success",
			"data": question,
		})
	}
}

// 按标题搜索提问箱中的某一提问

func SearchQuesFromBox() gin.HandlerFunc {
	return func(c *gin.Context) {
		bid := c.DefaultQuery("bid", "")
		title := c.DefaultQuery("title", "")
		page_num1 := c.DefaultQuery("pageNum", "")
		page_size1 := c.DefaultQuery("pageSize", "")
		id, _ := strconv.Atoi(bid)
		page_num, _ := strconv.Atoi(page_num1)
		page_size, _ := strconv.Atoi(page_size1)
		questions, err := service.SearchQuesFromBox(id, title)
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 1,
				"msg":  "fail to search the question",
				"data": nil,
			})
			return
		}
		length := len(*questions)
		var question []entity.QuestionInfo
		if (page_num+1)*page_size >= length {
			question = (*questions)[page_num*page_size : length]
		} else {
			question = (*questions)[page_num*page_size : (page_num+1)*page_size]
		}
		type QuesData struct {
			Total     int                   `json:"total"`
			Questions []entity.QuestionInfo `json:"questions"`
		}
		data := QuesData{
			Total:     length,
			Questions: question,
		}
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "success",
			"data": data,
		})
	}
}

//删除提问

func DeleteQues() gin.HandlerFunc {
	return func(c *gin.Context) {
		id := c.DefaultQuery("id", "") //接收id
		err := service.DeleteQues(id)  //调用service函数删除提问
		if err != nil {                //删除失败
			c.JSON(http.StatusOK, gin.H{
				"code": 1,
				"msg":  "fail to delete question",
				"data": nil,
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{ //success
			"code": 0,
			"msg":  "success",
			"data": nil,
		})
	}
}

type QLikeData struct {
	Aid int `json:"aid"`
	Qid int `json:"qid"`
}

// 点赞提问
func LikeQuesOrDeleteLike() gin.HandlerFunc {
	return func(c *gin.Context) {
		var QLikeD QLikeData
		if err := c.BindJSON(&QLikeD); err != nil { //接收点赞操作的信息
			c.JSON(http.StatusOK, gin.H{ //接受失败
				"code": 1,
				"msg":  "error to get json body",
				"data": nil,
			})
			return
		}
		err := service.LikeQuesOrDeleteLike(QLikeD.Aid, QLikeD.Qid) //调用service层函数进行操作
		if err != nil {
			c.JSON(http.StatusOK, gin.H{ //调用service层失败
				"code": 1,
				"msg":  "error to Like Or Delete Like",
				"data": nil,
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{ //success
			"code": 0,
			"msg":  "success",
			"data": nil,
		})
	}
}
